package com.purengage.ipilih;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;
import com.purengage.ipilih.lib.CustomStatusAdapter;
import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.HttpConnect;
import com.purengage.ipilih.lib.Msg;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Group extends ListActivity {

	ProgressBar progress;
	DBClass db;

	public class readGroup extends AsyncTask<String, String, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			
			String dateModif = (db.getDateMsg(3).equalsIgnoreCase("")) ? "2013-11-1"
					: db.getDateMsg(3);

			String uri = "http://purengage.com/api/statuses?client_id="
					+ getResources().getString(R.string.client_id) + "&token="
					+ db.getValue("token") + "&to=group&indent=" + params[0]
					+ "&count=" + params[1] + "&since_date_modified=" + dateModif.replaceAll("\\s+", "%20");
			
			HttpConnect http = new HttpConnect(uri);
			String output = "";

			boolean res = http.getData();
			if (res) {
				output = http.getOutput();
				try {
					JSONObject jobj = new JSONObject(output);
					JSONArray json = jobj.getJSONArray("data");

					for (int i = 0; i < json.length(); i++) {
						JSONObject job = json.getJSONObject(i);

						String user = (job.getString("from_volunteer_id")
								.equals("0") || job.getString(
								"from_volunteer_name").equalsIgnoreCase("null")) ? job
								.getString("from_user_name") : job
								.getString("from_volunteer_name");

						Msg m = new Msg();
						m.setId(job.getInt("id"));
						m.setMessage(job.getString("message"));
						m.setDate(job.getString("date_modified"));
						m.setUser(user);
						m.setMode(3);

						if (job.getString("id_prov").equalsIgnoreCase(
										db.getValue("id_prov"))
								&& job.getString("id_kab").equalsIgnoreCase(
										db.getValue("id_kab"))
								&& job.getString("kecamatan_id")
										.equalsIgnoreCase(
												db.getValue("kecamatan_id"))
								&& job.getString("desa_id").equalsIgnoreCase(
										db.getValue("desa_id")))
							db.insertMsg(m);

					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			return res;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			((PullToRefreshListView) getListView()).onRefreshComplete();

			progress.setVisibility(View.GONE);
			if (!result)
				Toast.makeText(
						Group.this,
						"Tidak dapat menghubungi server, periksa setting internet anda",
						Toast.LENGTH_LONG).show();
			else
				viewList();

		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group);

		progress = (ProgressBar) findViewById(R.id.statusProgress);
		db = new DBClass(Group.this);

		((PullToRefreshListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {
					public void onRefresh() {
						String[] params = { "1", "10" };
						new readGroup().execute(params);

					}
				});

		List<Msg> msg = db.getMsg(3);
		if (msg.size() > 0)
			viewList();
		else {
			String[] params = { "1", "10" };
			new readGroup().execute(params);
		}

	}

	private void viewList() {
				
		
		progress.setVisibility(View.GONE);
		List<Msg> msg = db.getMsg(3);
		CustomStatusAdapter statusAdapter = new CustomStatusAdapter(msg);

		setListAdapter(statusAdapter);
		registerForContextMenu(getListView());
	}

}
